//
//  UITextField+Category.m
//  TextInputLimitTest
//
//  Created by David on 16/1/14.
//  Copyright © 2016年 orangeCat. All rights reserved.
//

#import "UITextField+Category.h"
#import "NSString+Category.h"

@implementation UITextField (Limit)

- (void)limitTextFieldWithBytesLength:(NSInteger)maxLength {
    NSString* toBeString = self.text;
    //获取高亮部分
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
    if(!position){
        if (toBeString.byteLength > maxLength) {
            [self showAlerV];
        }
        [self cutTextFieldString:toBeString bytesLength:maxLength];
    }
}

//需要在 textField: shouldChangeCharactersInRange: replacementString: 方法中实现
- (BOOL)isEnabledWithBytesLength:(NSInteger)maxLength shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    
    NSString* toBeString = [self.text stringByReplacingCharactersInRange:range withString:string];
    
    
    NSUInteger charLen = toBeString.byteLength;
    if (charLen > maxLength) {
        
        if (![string isEqualToString:@""]) {
            if (self.text.byteLength != maxLength) {
                [self cutTextFieldString:toBeString bytesLength:maxLength];
            }
            //获取高亮部分
            UITextRange *selectedRange = [self markedTextRange];
            UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
            if(position){
                return YES;
            }else{
                [self showAlerV];
                return NO;
            }
            
        }
    }
    return YES;
}

- (void)cutTextFieldString:(NSString *)textString bytesLength:(NSInteger)maxLength {
    
    NSString *toBeString = self.text;
    NSString *lang = [self.textInputMode primaryLanguage];
    UITextRange *tmpRange = self.selectedTextRange;//记录下开始时候的光标位置
    
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [self markedTextRange];
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            while (toBeString.byteLength > maxLength && toBeString.length > 1) {
                
                toBeString = [toBeString substringToIndex:toBeString.length - 1];
                
            }
            self.text = toBeString;
            self.selectedTextRange = tmpRange;
            
            /*
             //            if (toBeString.byteLength > maxLength)
             //            {
             //
             //                //判断第三方中文输入法的emoji表情
             //                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
             //                if (rangeIndex.length == 1)
             //                {
             //                    self.text = [toBeString substringToIndex:maxLength];
             //                }
             //                else
             //                {
             //                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
             //                    self.text = [toBeString substringWithRange:rangeRange];
             //                }
             //            }
             *///暂时屏蔽
        }
    }
    // 中文输入法以外（英文和emoji）的直接对其统计限制即可
    else
    {
        if (toBeString.byteLength > maxLength)
        {
            while (toBeString.byteLength > maxLength) {
                if (toBeString.length > 1) {
                    toBeString = [toBeString substringToIndex:toBeString.length - 1];
                }
            }
            self.text = toBeString;
            self.selectedTextRange = tmpRange;
            /*
             //            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
             //            if (rangeIndex.length == 1)
             //            {
             //                self.text = [toBeString substringToIndex:maxLength];
             //            }
             //            else
             //            {
             //                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
             //                self.text = [toBeString substringWithRange:rangeRange];
             //            }
             *///暂时屏蔽
        }
    }

}


- (void)showAlerV
{
    UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"字数达到最高限制" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [aler show];
}




@end
